﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WpfApp5_StackPanel
{
    /// <summary>
    /// UsingYButton.xaml 的交互逻辑
    /// </summary>
    public partial class UsingYButton : Window
    {
        private const string Head1 = "Routed Event";
        private const string Head2 = "Sender";
        private const string Head3 = "Source";
        private const string Head4 = "Original Source";
        string outputFormat = "{0,-40}{1,-30}{2,-30}{3,-30}";
        public UsingYButton()
        {
            InitializeComponent();
            EventOutputHeader.Content = string.Format(outputFormat, Head1, Head2, Head3, Head4);
        }

        private void RoutedEventHandler(object sender, RoutedEventArgs rea)
        {
            TextBlock text = new TextBlock();
            text.Text = string.Format(outputFormat, rea.RoutedEvent.Name, GetTypeName(sender), GetTypeName(rea.Source),
                                      GetTypeName(rea.OriginalSource));

            EventOutput.Children.Add(text);
            (EventOutput.Parent as ScrollViewer).ScrollToBottom();
            //while(EventOutput.Children.Count > 20)
            //{
            //    EventOutput.Children.RemoveAt(0);
            //}
            if (EventOutput.Children.Count > 100)
            {
                EventOutput.Children.Clear();
            }
        }

        private string GetTypeName(object original)
        {
            string[] name = original.GetType().ToString().Split('.');
            return name[name.Length - 1];
        }

        private void OnPreviewYclick(object sender, RoutedEventArgs e)
        {
            RoutedEventHandler(sender, e);
        }

        private void OnYclick(object sender, RoutedEventArgs e)
        {
            RoutedEventHandler(sender, e);
        }
    }
}
